Option Strict Off
Option Explicit On
Module mod_multiROM
    '
    ' Communication Library for multiROM Memory Emulator
    ' Copyright (c) 2001 SofTec Microsystems
    '
    Friend m_live As Boolean = False
    Friend m_sixTeenBit As Boolean = False
    Friend m_TestThread As Int16 = 0
    Friend m_traceStart As Integer
    Friend m_traceEnd As Integer
    Friend Const MR_LPT1 As Short = 1
    Friend Const MR_LPT2 As Short = 2
    Friend Const MR_LPT3 As Short = 3
    Friend Const MR_LPT4 As Short = 4
    Friend Const MR_TRACE_BUFFER_SIZE As Short = 512

    ' MR_ErrorCodeT
    Friend Enum MR_ErrorCodeT
        MR_NO_ERRORS = 0
        MR_ERR_COM_CONNECTION = 1
        MR_ERR_COM_CFG_INSTRUMENT = 2
        MR_ERR_COM_DATA_TRANSFER = 3
        MR_ERR_INVALID_MODEL = 4
        MR_ERR_ADC = 5
        MR_ERR_IO_DRIVER = 6
        MR_ERR_BUSY = 7
        MR_ERR_MEM_PC_ALLOC = 8
        MR_ERR_MEM_LOAD_ADDR = 9
        MR_ERR_MEM_LOAD_CHK = 10
        MR_ERR_TRACE_BUSY = 11
        MR_ERR_TRACE_BUFF_SIZE = 12
        MR_ERR_TRACE_RANGE = 13
        MR_ERR_TRACE_TRG_ADDR = 14
        MR_ERR_TRG_MODE = 15
        MR_ERR_TRG_ADDR = 16
        MR_ERR_VTERM_BASE_ADDR = 17
        MR_ERR_EMU_INVALID_MEM = 18
        MR_ERR_EMU_NO_LIVE = 19
        MR_ERR_TEST_IO_POD = 20
        MR_ERR_TEST_ADC = 21
        MR_ERR_TEST_RAM = 22
        MR_ERR_VOLTAGE = 23
        MR_ERR_BANK_SWITCHING = 24
    End Enum

    ' MR_EmulatorModelT
    Friend Enum MR_EmulatorModelT
        MR_MODEL_MULTIROM8 = 0
        MR_MODEL_MULTIROM8L = 1
        MR_MODEL_MULTIROM16 = 2
        MR_MODEL_MULTIROM16L = 3
    End Enum

    ' MR_MemOrgT
    Friend Enum MR_MemOrgT
        MR_MEM_8Kx8 = 0
        MR_MEM_16Kx8 = 1
        MR_MEM_32Kx8 = 2
        MR_MEM_64Kx8 = 3
        MR_MEM_128Kx8 = 4
        MR_MEM_256Kx8 = 5
        MR_MEM_512Kx8 = 6
        MR_MEM_1Mx8 = 7
        MR_MEM_2Mx8 = 8
        MR_MEM_8Kx16 = 9
        MR_MEM_16Kx16 = 10
        MR_MEM_32Kx16 = 11
        MR_MEM_64Kx16 = 12
        MR_MEM_128Kx16 = 13
        MR_MEM_256Kx16 = 14
        MR_MEM_512Kx16 = 15
        MR_MEM_1Mx16 = 16
    End Enum

    ' MR_LedModeT
    Friend Enum MR_LedModeT
        MR_LED_NORMAL = 0
        MR_LED_BLINK_SLOW = 1
        MR_LED_BLINK_FAST = 2
    End Enum

    ' MR_MemTypeT
    Friend Enum MR_MemTypeT
        MR_MEM_EPROM = 0
        MR_MEM_FLASH = 1
    End Enum

    ' MR_ResetLevelT
    Friend Enum MR_ResetLevelT
        MR_RESET_MODE_ACTIVE_LOW = 0
        MR_RESET_MODE_ACTIVE_HIGH = 1
    End Enum

    ' MR_ResetEnableT
    Friend Enum MR_ResetEnableT
        MR_RESET_DISABLED = 0
        MR_RESET_ENABLED = 1
    End Enum

    ' MR_EmulationModeT
    Friend Enum MR_EmulationModeT
        MR_EMULATION_NORMAL = 0
        MR_EMULATION_REAL_TIME = 1
    End Enum

    ' MR_VTermEnableT
    Friend Enum MR_VTermEnableT
        MR_VTERM_DISABLED = 0
        MR_VTERM_ENABLED = 1
    End Enum

    ' MR_TrgOutEnableT
    Friend Enum MR_TrgOutEnableT
        MR_TRG_OUT_DISABLED = 0
        MR_TRG_OUT_ENABLED = 1
    End Enum

    ' MR_TrgOutModeT
    Friend Enum MR_TrgOutModeT
        MR_TRG_OUT_MODE_PULSE = 0
        MR_TRG_OUT_MODE_LATCH = 1
    End Enum

    ' MR_TrgTraceEnableT
    Friend Enum MR_TrgTraceEnableT
        MR_TRG_TRACE_DISABLED = 0
        MR_TRG_TRACE_ENABLED = 1
    End Enum

    ' MR_TraceModeT
    Friend Enum MR_TraceModeT
        MR_TRACE_MODE_NORMAL = 0
        MR_TRACE_MODE_CIRCULAR = 1
    End Enum

    ' MR_CommModeT
    Friend Enum MR_CommModeT
        MR_COMM_EPP = 0
        MR_COMM_BIDIR = 1
        MR_COMM_NIBBLE = 2
    End Enum

    Friend Declare Function MR_InitInstrument Lib "multiROM.DLL" (ByVal boardaddr As Short, ByVal callback As Integer) As Boolean
    'UPGRADE_WARNING: Structure MR_EmulatorModelT may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    Friend Declare Function MR_GetInstrumentID Lib "multiROM.DLL" (ByRef model As MR_EmulatorModelT) As Boolean
    Friend Declare Function MR_CloseCommunication Lib "multiROM.DLL" () As Boolean
    Friend Declare Function MR_GetNumLPTPorts Lib "multiROM.DLL" () As Integer
    Friend Declare Function MR_GetLPTBaseAddress Lib "multiROM.DLL" (ByVal nlpt As Integer) As Short
    'UPGRADE_WARNING: Structure MR_CommModeT may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    Friend Declare Function MR_GetLPTCommMode Lib "multiROM.DLL" (ByRef mode As MR_CommModeT) As Boolean
    Friend Declare Function MR_GetError Lib "multiROM.DLL" () As MR_ErrorCodeT
    Friend Declare Function MR_CheckIntrumentConnection Lib "multiROM.DLL" () As Boolean

    'UPGRADE_WARNING: Structure MR_LedModeT may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    Friend Declare Function MR_LedSetMode Lib "multiROM.DLL" (ByVal mode As MR_LedModeT) As Boolean
    'UPGRADE_WARNING: Structure MR_EmulationModeT may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    'UPGRADE_WARNING: Structure MR_MemTypeT may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    'UPGRADE_WARNING: Structure MR_MemOrgT may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    Friend Declare Function MR_DeviceConfig Lib "multiROM.DLL" (ByVal mem_org As MR_MemOrgT, ByVal mem_type As MR_MemTypeT, ByVal emul_mode As MR_EmulationModeT) As Boolean
    'UPGRADE_WARNING: Structure MR_ResetLevelT may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    Friend Declare Function MR_ResetConfig Lib "multiROM.DLL" (ByVal level As MR_ResetLevelT, ByVal dly_ms As Integer) As Boolean
    Friend Declare Function MR_VoltageConfig Lib "multiROM.DLL" (ByVal tenths_of_volt As Byte) As Boolean

    Friend Declare Function MR_TargetGetVoltage Lib "multiROM.DLL" (ByRef tenths_of_volt As Byte) As Boolean

    Friend Declare Function MR_Reset Lib "multiROM.DLL" () As Boolean
    Friend Declare Function MR_EmulationStart Lib "multiROM.DLL" () As Boolean
    Friend Declare Function MR_EmulationStop Lib "multiROM.DLL" () As Boolean

    Friend Declare Function MR_BufferWrite8 Lib "multiROM.DLL" (ByVal addr As Integer, ByRef src As Byte, ByVal length As Integer, ByVal callback As Integer) As Boolean
    Friend Declare Function MR_BufferWrite16 Lib "multiROM.DLL" (ByVal addr As Integer, ByRef src As Byte, ByVal length As Integer, ByVal callback As Integer) As Boolean
    Friend Declare Function MR_BufferEdit Lib "multiROM.DLL" () As Boolean
    Friend Declare Function MR_BufferUpdate Lib "multiROM.DLL" (ByVal callback As Integer) As Boolean

    'UPGRADE_WARNING: Structure MR_TrgOutModeT may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    'UPGRADE_WARNING: Structure MR_TrgOutEnableT may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    Friend Declare Function MR_TriggerOutConfig Lib "multiROM.DLL" (ByVal enable As MR_TrgOutEnableT, ByVal addr As Integer, ByVal mode As MR_TrgOutModeT) As Boolean
    Friend Declare Function MR_TriggerOutGetStatus Lib "multiROM.DLL" (ByRef status As Byte) As Boolean

    'UPGRADE_WARNING: Structure MR_VTermEnableT may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    Friend Declare Function MR_VTermConfig Lib "multiROM.DLL" (ByVal enable As MR_VTermEnableT, ByVal base_addr As Integer) As Boolean
    Friend Declare Function MR_VTermIsRxBufferEmpty Lib "multiROM.DLL" (ByRef status As Byte) As Boolean
    Friend Declare Function MR_VTermIsTxBufferFull Lib "multiROM.DLL" (ByRef status As Byte) As Boolean
    Friend Declare Function MR_VTermGetChar Lib "multiROM.DLL" (ByRef data As Byte) As Boolean
    Friend Declare Function MR_VTermPutChar Lib "multiROM.DLL" (ByVal data As Byte) As Boolean

    Friend Declare Function MR_TraceStart Lib "multiROM.DLL" () As Boolean
    Friend Declare Function MR_TraceStop Lib "multiROM.DLL" () As Boolean
    'UPGRADE_WARNING: Structure MR_TraceModeT may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    Friend Declare Function MR_TraceSetBuffer Lib "multiROM.DLL" (ByVal size As Integer, ByVal mode As MR_TraceModeT) As Boolean
    Friend Declare Function MR_TraceSetRange Lib "multiROM.DLL" (ByVal min_addr As Integer, ByVal max_addr As Integer) As Boolean
    'UPGRADE_WARNING: Structure MR_TrgTraceEnableT may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    Friend Declare Function MR_TraceSetTrigger Lib "multiROM.DLL" (ByVal enable As MR_TrgTraceEnableT, ByVal addr As Integer) As Boolean
    Friend Declare Function MR_TraceReadData Lib "multiROM.DLL" (ByRef mem As Integer, ByRef nelem As Integer) As Boolean
    Friend Declare Function MR_TraceGetStatus Lib "multiROM.DLL" (ByRef status As Byte, ByRef nelem As Integer) As Boolean
    Friend Declare Function MR_PodTestIO Lib "multiROM.DLL" (ByVal callback As Integer) As Boolean

    Friend Declare Sub MR_Delay Lib "multiROM.DLL" (ByVal delay_ms As Integer)
    'UPGRADE_WARNING: Structure MR_ResetEnableT may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    Friend Declare Function MR_SetResetIO Lib "multiROM.DLL" (ByVal state As MR_ResetEnableT) As Boolean
    Friend Declare Function MR_TriggerOutReset Lib "multiROM.DLL" () As Boolean
    Friend Declare Function MR_VTermClearBuffers Lib "multiROM.DLL" () As Boolean

    Friend Sub Upload_array(ByRef uArray() As Byte, ByVal Address_ As Integer, ByVal Length_ As Integer, ByVal targetAddress As Integer)

        ' Taking directly from uArray()... always update this array before calling this function!
        'UPGRADE_WARNING: Couldn't resolve default property of object m_live. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        If m_live Then
            If CBool(MR_BufferEdit()) = False Then ' get into edit mode
                Call Err_message("ERR" & MR_GetError())
                Threading.Thread.Sleep((500))
            End If

            If m_sixTeenBit Then ' if sixteenbit.. do funct and calc Address_'s etc
                Length_ = Length_ \ 2
                If Address_ Mod 2 = 1 Then
                    Address_ = Address_ - 1
                End If
                If targetAddress Mod 2 = 1 Then
                    targetAddress = targetAddress - 1
                End If
                targetAddress = targetAddress \ 2
                If CBool(MR_BufferWrite16(targetAddress, uArray(Address_), Length_, 0)) = True Then
                    Call Err_message("ERR" & MR_GetError())
                    Threading.Thread.Sleep((500))
                End If
            Else ' do 8bit operation etc
                If CBool(MR_BufferWrite8(targetAddress, uArray(Address_), Length_, 0)) = True Then
                    Call Err_message("ERR" & MR_GetError())
                    Threading.Thread.Sleep((500))
                End If
            End If

            If CBool(MR_BufferUpdate(0)) = False Then ' do update and output complete
                Call Err_message("ERR" & MR_GetError())
                Threading.Thread.Sleep((500))
            Else
                Call Err_message("Update Complete")
            End If

        End If
    End Sub

    Friend Sub Upload_Byte_Fill(ByVal adr As Integer, ByRef Valu As Byte, ByRef segLen As Integer)
        Dim i As Integer
        Dim addr As Integer
        Dim ValArr(2) As Byte
        ValArr(0) = Valu
        ValArr(1) = Valu

        If m_live Then
            If CBool(MR_BufferEdit()) = False Then ' get into edit mode
                Call Err_message("ERR" & MR_GetError())
                Threading.Thread.Sleep((500))
            End If

            If m_sixTeenBit Then ' if sixteenbit.. do funct and calc adr's etc
                segLen = segLen \ 2
                If adr Mod 2 = 1 Then
                    adr = adr - 1
                End If
                addr = adr \ 2

                For i = 0 To segLen
                    If CBool(MR_BufferWrite16(addr + i, ValArr(0), 1, 0)) = True Then
                        Call Err_message("ERR" & MR_GetError())
                        Threading.Thread.Sleep((500))
                    End If
                Next
            Else ' do 8bit operation etc
                For i = 0 To segLen
                    If CBool(MR_BufferWrite8(adr + i, ValArr(0), 1, 0)) = True Then
                        Call Err_message("ERR" & MR_GetError())
                        Threading.Thread.Sleep((500))
                    End If
                Next
            End If

            If CBool(MR_BufferUpdate(0)) = False Then ' do update and output complete
                Call Err_message("ERR" & MR_GetError())
                Threading.Thread.Sleep((500))
            Else
                Call Err_message("Update Complete")
            End If
        End If
    End Sub
    Private Sub tmrTrace_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs)
        Dim n_elementi(1) As Integer
        Dim i, ro, co As Short
        Dim codee As Byte

        Dim m_emuTraceBuffer(0 To 10) As Byte

        If CBool(MR_TraceStop()) = False Then
            codee = MR_GetError()
            Call Err_message("ERR" & Str(codee))
        End If

        If CBool(MR_TraceReadData(m_emuTraceBuffer(0), n_elementi(0))) = False Then
            codee = MR_GetError()
            Call Err_message("ERR" & Str(codee))
        End If

        If n_elementi(0) > 4 Then
            'Call Trace_Show(Fuel, t_offsets, False)
            For i = 0 To 3
                If m_sixTeenBit Then
                    't_offsets(i) = (m_emuTraceBuffer(i) - m_traceStart) * 2
                Else
                    't_offsets(i) = m_emuTraceBuffer(i) - m_traceStart
                End If
            Next
            'Call Trace_Show(Fuel, t_offsets, True)
        End If

        If m_sixTeenBit Then
            For i = 0 To 3

                If m_emuTraceBuffer(i) <> 0 Then
                    ro = (((m_emuTraceBuffer(i) - m_traceStart) * 2) \ 16) + 1
                    co = (((m_emuTraceBuffer(i) - m_traceStart) * 2) Mod 16) + 1

                    If ro > 16 Then
                        ro = 15
                    End If
                    ' m_traceBuffer(co, ro) = True
                    ' m_traceBuffer(co + 1, ro) = True
                End If
            Next
        Else
            For i = 0 To 3
                If m_emuTraceBuffer(i) <> 0 Then
                    ro = ((m_emuTraceBuffer(i) - m_traceStart) \ 16) + 1
                    co = ((m_emuTraceBuffer(i) - m_traceStart) Mod 16) + 1
                    If ro > 16 Then
                        ro = 16
                    End If
                    ' m_traceBuffer(co, ro) = True
                End If
            Next
        End If

        If CBool(MR_TraceStart()) = False Then
            Call Err_message("Error starting trace")
        End If
    End Sub
    Friend Sub Init_UploadAll()
        Dim offset As Integer
        Dim i As Integer
        Dim addr As Integer

        'on error goto UploadErr
        offset = 0
        addr = 0

        If CBool(MR_CheckIntrumentConnection) = True Then
            Call Err_message("Connection Not Present")
        Else
            If m_sixTeenBit Then ' 16 bit operations
                If CBool(MR_BufferEdit()) = False Then
                    Call Err_message("ERR" & MR_GetError())
                    ' Sleep((500))
                End If
                ' For addr = 0 To m_fileLength - 1024 Step 1024
                ' Call ShowStatusBarPercent((addr / m_fileLength) * 100)
                i = addr / 2
                ' If CBool(MR_BufferWrite16(i, b_main(addr), 512, 0)) = True Then
                'Call Err_message("ERR" & MR_GetError())
                'End If
                'Next
                If CBool(MR_BufferUpdate(0)) = False Then
                    Call Err_message("ERR" & MR_GetError())
                Else
                    Call Err_message("Update Complete!")
                End If
            Else
                If CBool(MR_BufferEdit()) = False Then
                    Call Err_message("ERR" & MR_GetError())
                End If
                ' For addr = 0 To m_fileLength - 1024 Step 1024
                'Call ShowStatusBarPercent((addr / m_fileLength) * 100)
                'If CBool(MR_BufferWrite8(addr, b_main(addr), 1024, 0)) = True Then
                'Call Err_message("ERR" & MR_GetError())
                'End If
                'Next
                If CBool(MR_BufferUpdate(0)) = False Then
                    Call Err_message("ERR" & MR_GetError())
                    '  Sleep((500))
                Else
                    Call Err_message("Update Complete")
                End If
            End If
        End If
        Exit Sub
UploadErr:
        Err_message("Uploading Error!")

    End Sub
End Module